import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AddUserProfileFields1730621000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 添加个人信息字段
    await queryRunner.addColumns('users', [
      new TableColumn({
        name: 'avatar',
        type: 'varchar',
        length: '500',
        isNullable: true,
        comment: '头像URL',
      }),
      new TableColumn({
        name: 'gender',
        type: 'enum',
        enum: ['male', 'female', 'other'],
        isNullable: true,
        comment: '性别',
      }),
      new TableColumn({
        name: 'birthday',
        type: 'date',
        isNullable: true,
        comment: '生日',
      }),
      new TableColumn({
        name: 'bio',
        type: 'text',
        isNullable: true,
        comment: '个人简介',
      }),
    ]);

    // 添加统计信息字段
    await queryRunner.addColumns('users', [
      new TableColumn({
        name: 'likesCount',
        type: 'int',
        default: 0,
        comment: '点赞数（用户点赞别人的总数）',
      }),
      new TableColumn({
        name: 'receivedLikesCount',
        type: 'int',
        default: 0,
        comment: '获赞数（用户作品被点赞的总数）',
      }),
      new TableColumn({
        name: 'followingCount',
        type: 'int',
        default: 0,
        comment: '关注数',
      }),
      new TableColumn({
        name: 'followersCount',
        type: 'int',
        default: 0,
        comment: '粉丝数',
      }),
      new TableColumn({
        name: 'collectionsCount',
        type: 'int',
        default: 0,
        comment: '收藏作品数',
      }),
      new TableColumn({
        name: 'worksCount',
        type: 'int',
        default: 0,
        comment: '发布作品数',
      }),
    ]);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 删除统计信息字段
    await queryRunner.dropColumn('users', 'worksCount');
    await queryRunner.dropColumn('users', 'collectionsCount');
    await queryRunner.dropColumn('users', 'followersCount');
    await queryRunner.dropColumn('users', 'followingCount');
    await queryRunner.dropColumn('users', 'receivedLikesCount');
    await queryRunner.dropColumn('users', 'likesCount');

    // 删除个人信息字段
    await queryRunner.dropColumn('users', 'bio');
    await queryRunner.dropColumn('users', 'birthday');
    await queryRunner.dropColumn('users', 'gender');
    await queryRunner.dropColumn('users', 'avatar');
  }
}
